package com.hkts.naturalgas.flow.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.flow.entity.FlowHistory;
import com.hkts.naturalgas.flow.entity.FlowTaskInstance;
import com.hkts.naturalgas.flow.mapper.FlowHistoryMapper;
import com.hkts.naturalgas.flow.service.IFlowHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-08-31
 */
@Service
@Transactional
public class FlowHistoryServiceImpl extends ServiceImpl<FlowHistoryMapper, FlowHistory> implements IFlowHistoryService {


    @Autowired
    private FlowHistoryMapper flowHistoryMapper;

    @Override
    public IPage<FlowHistory> selectPage(Page<FlowHistory> page, FlowHistory flowHistory) {
        QueryWrapper<FlowHistory> query = Wrappers.query();
        QueryWrapper<FlowHistory> entityWrapper = getEntityWrapper(query, flowHistory);
        return flowHistoryMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<FlowHistory> selectList(FlowHistory flowHistory) {
        QueryWrapper<FlowHistory> query = Wrappers.query();
        QueryWrapper<FlowHistory> entityWrapper = getEntityWrapper(query, flowHistory);
        return flowHistoryMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<FlowHistory> getEntityWrapper(QueryWrapper<FlowHistory> entityWrapper, FlowHistory flowHistory) {
        entityWrapper.lambda().eq(StringUtils.isNotBlank(flowHistory.getFlowInstanceId()),FlowHistory::getFlowInstanceId,flowHistory.getFlowInstanceId());

        return entityWrapper;
    }

    @Override
    public FlowHistory getFlowHistoryByTaskId(String taskId,String taskCode,String nodeId) {
        QueryWrapper<FlowHistory> query = Wrappers.query();
        query.lambda().eq(FlowHistory::getFlowTaskId,taskId)
                .eq(FlowHistory::getFlowNodeId,nodeId)
        .eq(FlowHistory::getFlowTaskCode,taskCode);

        return flowHistoryMapper.selectOne(query);
    }

    @Override
    public void addFlowHistory(FlowTaskInstance flowTaskInstance,Integer setpId) {
        FlowHistory flowHistory=new FlowHistory();
        flowHistory.setFlowInstanceId(flowTaskInstance.getFlowInstanceId());
        flowHistory.setFlowNodeId(flowTaskInstance.getFlowNodeId());
        flowHistory.setFlowTaskId(flowTaskInstance.getId());
        flowHistory.setTaskCreateTime(LocalDateTime.now());
        flowHistory.setFlowTaskCode(flowTaskInstance.getFlowTaskCode());
        flowHistory.setSetpId(setpId);
        flowHistoryMapper.insert(flowHistory);
    }
}
